//
//  WalkNaviViewController.m
//  CarLifes
//
//  Created by yu on 2017/7/20.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "WalkNaviViewController.h"
#import "MoreMenuView.h"
#import "KLCPopup.h"

@interface WalkNaviViewController ()<AMapNaviWalkViewDelegate,MoreMenuViewDelegate>

@property (nonatomic, strong) MoreMenuView *moreMenu;

@property (nonatomic, strong) KLCPopup *popup;

@end

@implementation WalkNaviViewController

#pragma mark - Life Cycle

- (instancetype)init
{
    if (self = [super init])
    {
        [self initWalkView];
        
        [self initMoreMenu];
        
        [self setupPopupView];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    
    [self.walkView setFrame:self.view.bounds];
    [self.view addSubview:self.walkView];
}

- (void)initWalkView
{
    if (self.walkView == nil)
    {
        self.walkView = [[AMapNaviWalkView alloc] init];
        self.walkView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self.walkView setCarImage:IMAGE_NAMED(@"userPosition")];
        [self.walkView setDelegate:self];
    }
}

- (void)initMoreMenu
{
    if (self.moreMenu == nil)
    {
        self.moreMenu = [[MoreMenuView alloc] init];
        self.moreMenu.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        [self.moreMenu setSize:CGSizeMake(kScreenWidth, kIsIphoneX ? 220 : 200)];
        [self.moreMenu setDelegate:self];
    }
}

- (void)setupPopupView
{
    if (self.popup == nil) {
        self.popup = [KLCPopup popupWithContentView:self.moreMenu
                                           showType:KLCPopupShowTypeSlideInFromBottom
                                        dismissType:KLCPopupDismissTypeSlideOutToBottom
                                           maskType:KLCPopupMaskTypeDimmed
                           dismissOnBackgroundTouch:YES
                              dismissOnContentTouch:NO];
    }
}

#pragma mark - WalkView Delegate
/**
 * @brief 导航界面关闭按钮点击时的回调函数
 */
- (void)walkViewCloseButtonClicked:(AMapNaviWalkView *)walkView
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您确定退出导航吗？" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(walkNaviViewCloseButtonClicked)]) {
            [self.delegate walkNaviViewCloseButtonClicked];
        }
    }];
    
    [alert addAction:cancelAction];
    [alert addAction:confirmAction];
    
    [self presentViewController:alert animated:YES completion:nil];
}

/**
 * @brief 导航界面更多按钮点击时的回调函数
 * @param walkView 步行导航界面
 */
- (void)walkViewMoreButtonClicked:(AMapNaviWalkView *)walkView
{
    // 配置MoreMenu状态
    [self.moreMenu setTrackingMode:self.walkView.trackingMode];
    [self.moreMenu setShowNightType:self.walkView.showStandardNightType];
    
    [self.popup showWithLayout:KLCPopupLayoutMake(KLCPopupHorizontalLayoutCenter, KLCPopupVerticalLayoutBottom)];
}

/**
 * @brief 导航界面转向指示View点击时的回调函数
 */
- (void)walkViewTrunIndicatorViewTapped:(AMapNaviWalkView *)walkView
{
    if (self.walkView.showMode == AMapNaviDriveViewShowModeCarPositionLocked)
    {
        [self.walkView setShowMode:AMapNaviDriveViewShowModeNormal];
    }
    else if (self.walkView.showMode == AMapNaviDriveViewShowModeNormal)
    {
        [self.walkView setShowMode:AMapNaviDriveViewShowModeOverview];
    }
    else if (self.walkView.showMode == AMapNaviDriveViewShowModeOverview)
    {
        [self.walkView setShowMode:AMapNaviDriveViewShowModeCarPositionLocked];
    }
}

/**
 * @brief 导航界面显示模式改变后的回调函数
 * @param walkView 步行导航界面
 * @param showMode 显示模式
 */
- (void)walkView:(AMapNaviWalkView *)walkView didChangeShowMode:(AMapNaviWalkViewShowMode)showMode
{
    PPLog(@"walkDidChangeShowMode:%ld", (long)showMode);
}


#pragma mark - MoreMenu Delegate

- (void)moreMenuViewFinishButtonClicked {
    [self.popup dismiss:YES];
}

- (void)moreMenuViewNightTypeChangeTo:(BOOL)isShowNightType {
    [self.walkView setShowStandardNightType:isShowNightType];
}

- (void)moreMenuViewTrackingModeChangeTo:(AMapNaviViewTrackingMode)trackingMode {
    [self.walkView setTrackingMode:trackingMode];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
